package zm.irc.cmd;


import com.mysql.cj.util.StringUtils;

import org.apache.log4j.Logger;

import zm.irc.cfg.SayLoveApiConfig;
import zm.irc.client.IrcClient;
import zm.irc.dto.InterestingTextInfo;
import zm.irc.ioc.annotation.AutoWired;
import zm.irc.ioc.annotation.Service;
import zm.irc.message.receive.IrcReceiveCmdMessage;
import zm.irc.message.send.IrcChatMessage;
import zm.irc.msgqueue.LocalMemoryMsgQueue;
import zm.irc.util.HttpUtil;
import zm.irc.util.TianXinUtil;

/**
 * <pre>
 * 舔狗日记: Simp diary
 * </pre>
 */
@Service(name=CommandNameConst.CMD_SAY_LOVE)
public class CmdSayLove extends AbsCmdTianXin{
    private static final Logger log = Logger.getLogger(CmdSayLove.class);

    @AutoWired
    private LocalMemoryMsgQueue localMemoryMsgQueue ;

    @AutoWired
    private TianXinUtil tianXinUtil;

    @AutoWired
    private SayLoveApiConfig sayLove;

    public CmdSayLove(){
    }



    @Override
    protected InterestingTextInfo doExecute(IrcReceiveCmdMessage cmd, IrcClient client) {
        String params = "?key=" + this.sayLove.getKey();
        String msgStrJson = HttpUtil.getJson(sayLove.getUrl() + params, null);
        String msgContent = "";
        InterestingTextInfo interestingTextInfo = null;
        try{
            msgContent = tianXinUtil.getContent(msgStrJson);
           if(StringUtils.isEmptyOrWhitespaceOnly(msgContent)){
               throw new RuntimeException("Get say love message error!");
           }
           interestingTextInfo = new InterestingTextInfo("SayLove", String.valueOf(msgContent.hashCode()), msgContent, "tianxin");
        }catch(Exception e){
            log.error("Encoding error!",e);
        }
   
        IrcChatMessage msg = new IrcChatMessage();
        msg.setChannel(cmd.getChannel());
        msg.setMsg(msgContent);
        this.localMemoryMsgQueue.addSendQueue(msg);

        
        return interestingTextInfo;
    }

    
}
